package tt.com.datamanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.Hall;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.service.HallService;

@RestController
@RequestMapping(value = "hall/manager")
public class HallController {
    @Autowired
    private HallService hallService;;

    @PostMapping(value = "show/page")
    public CommonResult showAllHall(@RequestBody PageUtil pageUtil){
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null){
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        pageUtil = hallService.selectAllHallByPage(pageUtil);
        return CommonResult.buildR(Status.OK, "所有灵堂查询成功", pageUtil);
    }

    @PostMapping(value = "update")
    public CommonResult updateHall(@RequestBody Hall hall){
        if (hall == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (hallService.updateHall(hall, "1") > 0) {
            return CommonResult.buildR(Status.OK, "灵堂修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "灵堂修改失败");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult deleteHall(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = hallService.deleteHall(id,"1");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "灵堂不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "灵堂已删除");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "灵堂删除成功");
        }
    }

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody Hall hall) {
        if (!ObjectUtil.objectIsNull(hall)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (hall.getDayPriceProject()==null && hall.getTimePriceProject()==null){
            return CommonResult.buildR(Status.PARAM_ISNULL, "请设置灵堂收费标准");
        }
        if(hall.getHallType()==null){
            return CommonResult.buildR(Status.PARAM_ISNULL, "请设置灵堂类型");
        }
        Integer result = hallService.insertHall(hall);
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "不允许重复添加");
        } else if (result > 0) {
            return CommonResult.buildR(Status.OK, "灵堂添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "灵堂添加失败");
        }
    }

}
